﻿using System;
using System.Collections.Generic;
using System.Text;
using MeTalker.Model;
using MeTalker.DBUtility;
using System.Data;
using System.Data.SqlClient;

namespace MeTalker.DAL
{
   public class SoftwaresDAL:DALBase
   {
       public static int Delete(int autoid)
       {
           try
           {
               string sql = "delete  Softwares where AutoID=" + autoid;
               return SqlHelper.ExecuteNonQuery(conn, CommandType.Text, sql, null);
           }
           catch
           {
               return 0;
           }
       }

       public static int Delete(string autoid)
       {
           return Delete(int.Parse(autoid));
       }

       public static int Insert(SoftwaresMod softwares)
       {
           try
           {
               string sql = "insert into Softwares(SoftID,SoftName,Version,DownloadURL,UpdateInfo,TrialDays) values(@SoftID,@SoftName,@Version,@DownloadURL,@UpdateInfo,@TrialDays)";
               return SqlHelper.ExecuteNonQuery(conn, CommandType.Text, sql, GetSqlParameter(softwares));
           }
           catch
           {
               return 0;
           }
       }

       public static int Update(SoftwaresMod softwares)
       {
           try
           {
               string sql = "update Softwares set SoftID=@SoftID,SoftName=@SoftName,Version=@Version,DownloadURL=@DownloadURL,UpdateInfo=@UpdateInfo,TrialDays=@TrialDays where AutoID=@AutoID";
               return SqlHelper.ExecuteNonQuery(conn, CommandType.Text, sql, GetSqlParameter(softwares));
           }
           catch
           {
               return 0;
           }
       }

       static SoftwaresMod SetModel(DataRow row)
       {
           SoftwaresMod software = new SoftwaresMod();
           software.AutoID = int.Parse(row["AutoID"].ToString());
           software.DownloadURL = row["DownloadURL"].ToString();
           software.SoftID = row["SoftID"].ToString();
           software.SoftName = row["SoftName"].ToString();
           software.TrialDays = int.Parse(row["TrialDays"].ToString());
           software.UpdateInfo = row["UpdateInfo"].ToString();
           software.Version = row["Version"].ToString();
           return software;
       }

       public static List<SoftwaresMod> GetSoftwaresToList()
       {
           try
            {
                DataTable dt = SoftwaresDAL.GetSoftwares();
                if (dt.Rows.Count <= 0)
                {
                    return null;
                }
                List<SoftwaresMod> software = new List<SoftwaresMod>();
                foreach (DataRow row in dt.Rows)
                {
                    software.Add(SetModel(row));
                }
                return software;
            }
            catch
            {
                return null;
            }
       }

       public static DataTable GetSoftwares() {
           try
           {
               string sql = "select * from Softwares";
               return SqlHelper.ExecuteDataTable(sql, null);
           }
           catch
           {
               return null;
           }
       }

       public static DataTable GetSoftware(string softID)
       {
           try
           {
               string sql = "select * from Softwares where SoftID=@SoftID";
               SqlParameter paramSoftID = new SqlParameter("@SoftID", SqlDbType.VarChar, 100, "SoftID");
               paramSoftID.Value = softID;
               return SqlHelper.ExecuteDataTable(sql, new SqlParameter[] { paramSoftID });
           }
           catch
           {
               return null;
           }
       }
       public static DataTable GetSoftwareByAutoID(string autoID)
       {
           try
           {
               string sql = "select * from Softwares where AutoID=" + autoID;
               return SqlHelper.ExecuteDataTable(sql, null);
           }
           catch
           {
               return null;
           }
       }
       public static SoftwaresMod GetSoftwareByAutoIDToMod(string autoID) {
           try
           {
               DataTable dt = GetSoftwareByAutoID(autoID);
               if (dt.Rows.Count <= 0)
               {
                   return null;
               }
               List<SoftwaresMod> software = new List<SoftwaresMod>();
               foreach (DataRow row in dt.Rows)
               {
                   software.Add(SetModel(row));
               }
               if (software.Count <= 0)
                   return null;
               return software[0];
           }
           catch
           {
               return null;
           }
       }
       public static SoftwaresMod GetSoftwareBySoftID(string softID)
       {
           try
           {
               DataTable dt = GetSoftware(softID);
               if (dt.Rows.Count <= 0)
               {
                   return null;
               }
               List<SoftwaresMod> software = new List<SoftwaresMod>();
               foreach (DataRow row in dt.Rows)
               {
                   software.Add(SetModel(row));
               }
               if (software.Count <= 0)
                   return null;
               return software[0];
           }
           catch
           {
               return null;
           }
       }

       static SqlParameter[] GetSqlParameter(SoftwaresMod softwares)
       {
           List<SqlParameter> paramss = new List<SqlParameter>();
           SqlParameter paramAutoID = new SqlParameter("@AutoID", SqlDbType.Int, 4, "AutoID");
           paramAutoID.Value = softwares.AutoID;
           paramss.Add(paramAutoID);

           SqlParameter paramSoftID = new SqlParameter("@SoftID", SqlDbType.VarChar, 100, "SoftID");
           paramSoftID.Value = softwares.SoftID;
           paramss.Add(paramSoftID);

           SqlParameter paramSoftName = new SqlParameter("@SoftName", SqlDbType.VarChar, 100, "SoftName");
           paramSoftName.Value = softwares.SoftName;
           paramss.Add(paramSoftName);

           SqlParameter paramVersion = new SqlParameter("@Version", SqlDbType.VarChar, 100, "Version");
           paramVersion.Value = softwares.Version;
           paramss.Add(paramVersion);

           SqlParameter paramDownloadURL = new SqlParameter("@DownloadURL", SqlDbType.VarChar, 100, "DownloadURL");
           paramDownloadURL.Value = softwares.DownloadURL;
           paramss.Add(paramDownloadURL);

           SqlParameter paramUpdateInfo = new SqlParameter("@UpdateInfo", SqlDbType.VarChar, 100, "UpdateInfo");
           paramUpdateInfo.Value = softwares.UpdateInfo;
           paramss.Add(paramUpdateInfo);

           SqlParameter paramTrialDays = new SqlParameter("@TrialDays", SqlDbType.Int, 4, "TrialDays");
           paramTrialDays.Value = softwares.TrialDays;
           paramss.Add(paramTrialDays);

           return paramss.ToArray();
       }
    }
}
